package com.zmn.plat.business.impl.online.service.group;

import javax.annotation.Resource;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.business.interfaces.online.service.group.OnlineServiceGroupBService;
import com.zmn.plat.model.entity.online.service.group.OnlineServiceGroup;
import com.zmn.plat.model.entity.online.service.group.OnlineServiceGroupQuery;
import com.zmn.plat.services.interfaces.online.service.group.OnlineServiceGroupService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author linfeng
 * @description 在线客服分组BService实现类
 * @modifier
 * @since 2021/03/30 18:13
 **/
@Service
public class OnlineServiceGroupBServiceImpl implements OnlineServiceGroupBService {
    @Resource
    private OnlineServiceGroupService onlineServiceGroupService;

    @Override
    public Boolean judgeHasDefaultServiceGroup(OnlineServiceGroupQuery onlineServiceGroupQuery) {
        // 新增/修改时候判断选择的对接系统+子公司已经存在默认客服组时
        if (NumberUtil.isNullOrZero(onlineServiceGroupQuery.getGroupId())) {
            OnlineServiceGroupQuery query = new OnlineServiceGroupQuery();
            query.setSysId(onlineServiceGroupQuery.getSysId());
            query.setDefaultType(GlobalConsts.YES);
            query.setSubCompanyIdList(onlineServiceGroupQuery.getSubCompanyIdList());
            List<OnlineServiceGroup> onlineServiceGroupList = onlineServiceGroupService.listByQuery(query);
            if (CollectionUtil.isNotNullOrEmpty(onlineServiceGroupList)) {
                return true;
            }
        } else {
            OnlineServiceGroupQuery query = new OnlineServiceGroupQuery();
            query.setSysId(onlineServiceGroupQuery.getSysId());
            query.setDefaultType(GlobalConsts.YES);
            query.setSubCompanyIdList(onlineServiceGroupQuery.getSubCompanyIdList());
            List<OnlineServiceGroup> onlineServiceGroupList = onlineServiceGroupService.listByQuery(query);
            onlineServiceGroupList = onlineServiceGroupList.stream().filter(e -> !Objects.equals(onlineServiceGroupQuery.getGroupId(), e.getGroupId())).collect(Collectors.toList());
            if (CollectionUtil.isNotNullOrEmpty(onlineServiceGroupList)) {
                return true;
            }
        }

        return false;
    }
}